#
# @lc app=leetcode.cn id=141 lang=python3
#
# [141] 环形链表
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        # 初始化快慢指针指向头结点
        fast = slow = head
        while(fast is not None and fast.next is not None):
            # 快指针每次前进两步
            fast = fast.next.next
            # 慢指针每次前进一步
            slow = slow.next
            # 如果存在环，两指针必相遇
            if fast == slow:
                return True

        return False

# @lc code=end
